﻿using RsCode;
using Rswl.Common.Core.User.Aggregate.ValueObject;
using System.Security.Claims;

namespace Rswl.Common.Core.User.Aggregate.Entities
{
    public partial class UserInfo
    {
        public UserInfo()
        {

        }

        public UserInfo(string userName, string avatar, string nick,string phone="", string inviteUserId = "")
        {
            UserId = Guid.NewGuid().ToString("N");
            UserName = userName;
            Phone = phone;
            RegisterDate = DateTime.Now;
            Role = UserRole.Member;
            Grade = 0;
            Avatar = avatar ?? "";
            Nick = nick ?? "";
            TrueName = Nick;
            InviteUserId = inviteUserId;
        }
        /// <summary>
        /// 创建访客
        /// </summary>
        public void CreateVisitor()
        {
            UserId = Guid.NewGuid().ToString("N");
            UserName ="访客";
            Phone = "";
            RegisterDate = DateTime.Now;
            Role = UserRole.Visitor;
            Grade = 0;
            Avatar = "";
            Nick = "访客";
            InviteUserId = "";
        }

        public void ChangeNick(string name)
        {
			if (!string.IsNullOrWhiteSpace(name))
			{
				UserName = name; Nick = name;
			}
		}
        public void ChangeAvatar(string avatar)
        {
			if (!string.IsNullOrWhiteSpace(avatar) && avatar != "undefined")
			{
				Avatar = avatar;
			}
		}

        public void ChangeStatus(int status)
        {
            if(Role== UserRole.Admin)
            {
                throw new AppException("该帐号为管理员,当前操作无效");
            }
            Status= status;
        }

        public void Check()
        {
            if(Status==0)
            {
                throw new AppException("该帐号被禁用");
            }
        }

        string PasswordEncode(string password, string salt)
        {
            string str = SecureHelper.MD5(password);
            return SecureHelper.MD5(string.Concat(str, salt));
        }

        public void ChangeRole(UserRole userRole)
        {
            Role = userRole;
        }
        public void ChangeGrade(UserGrade userGrade, DateTime expirseDate)
        {
            //Grade = userGrade;
            VipEndDate = expirseDate;
        }

        public List<Claim> GetClaims()
        {
            List<Claim> claims = new List<Claim>();
            claims.Add(new Claim("UserId", UserId));
            claims.Add(new Claim("UserName", UserName));
            claims.Add(new Claim("Role", ((int)Role).ToString()));
            claims.Add(new Claim("Grade", ((int)Grade).ToString()));
            claims.Add(new Claim("Avatar", Avatar));
            claims.Add(new Claim("Nick", Nick));
            claims.Add(new Claim("InviteCode", InviteCode ?? ""));

            if (VipEndDate == null)
            {
                claims.Add(new Claim("VipEndDate", ""));
            }
            else
            {
                var date = Convert.ToDateTime(VipEndDate);
                claims.Add(new Claim("VipEndDate", date.ToString("yyyy-MM-dd HH:mm:ss")));
            }

            claims.Add(new Claim("RegisterDate", RegisterDate.ToString("yyyy-MM-dd HH:mm:ss")));

            return claims;
        }

        public void SetExtInfo(List<UserExtInfo> userExtInfos)
        {
            if (userExtInfos != null)
            {
                UserExtInfos = userExtInfos;
            }
        }

        public bool SetInviteUser(string inviteUserId)
        {
            if (string.IsNullOrWhiteSpace(InviteUserId))
            {
                InviteUserId = inviteUserId;
                return true;
            }
            return false;
        }

        public void SetUserRoles(string[] roles)
        {
            UserRoles =string.Join("," ,roles);
        }
        public void SetUserPhone(string phone)
        {
            Phone= phone;
        }
    }
}
